<?php

namespace App\Http\Controllers\Api;

use App\Handlers\ImageUploadHandler;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\ImageRequest;
use App\Http\Resources\ImageResource;
use App\Models\Image;
use Illuminate\Support\Str;


class ImagesController extends Controller
{
    //上传图片
    public function store(ImageRequest $request,ImageUploadHandler $uploadHandler)
    {
        $user = $request->user();
        $type = $request->type;

        $size = $type == 'avatar' ? 600 : 1024;
        $result = $uploadHandler->save($request->image, Str::plural($type), $user->id, $size);

        $image = Image::create([
            "user_id" => $user->id,
            "type" => $type,
            'path' => $result['path']
        ]);

        return new ImageResource($image);
    }

}
